package com.onlineshopping.service.impl;

import com.onlineshopping.entity.User;
import com.onlineshopping.exception.LoginException;
import com.onlineshopping.mapper.UserMapper;
import com.onlineshopping.service.UserService;
import com.onlineshopping.utils.JwtTokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;


    @Override
    public String login(User user) {
        String username = user.getUsername();
        User userDetails = userMapper.findByUsername(username);
        if (userDetails == null) {
            throw new LoginException("用户不存在");
        }
        if (!user.getPassword().equals(userDetails.getPassword())) {
            throw new LoginException("密码错误");
        }
        //log.info("User logged in successfully{}", username);
        return JwtTokenUtil.generateToken(user);
    }

    @Override
    public void resetPassword(User user) {
        userMapper.resetPassword(user);
    }

    @Override
    public String getProfile(String username) {
        return userMapper.getProfile(username);
    }

    @Override
    public boolean updateSetting(String username, String address, String oldUsername) {
        return userMapper.updateSetting(username, address, oldUsername) > 0;
    }

    @Override
    public List<User> getAll() {
        return userMapper.getAll();
    }

    @Override
    public void deleteUser(Long id) {
        userMapper.deleteUser(id);
    }
}
